package com.pro.service.imple;

import com.pro.mapper.HomePageMapper;
import com.pro.pojo.JsBeforeUser;
import com.pro.pojo.JsCourse;
import com.pro.pojo.JsProduct;
import com.pro.pojo.JsUser;
import com.pro.service.HomePageService;
import com.pro.utils.MD5Util;
import com.pro.utils.SecurityPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 李佳豪
 * @date 2022/1/7 9:02
 */
@Service
public class HomePageServiceImple implements HomePageService {
    @Autowired
    HomePageMapper homePageMapper;

    @Override
    public Map<String,Object>  getAllUsersRegistrationTime() {
        Integer todayUser = 0;//今天
        Integer yesterdayUser = 0;//昨天
        Integer monthUser = 0;//本月
        Integer AllUsers = 0;
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat date1 = new SimpleDateFormat("yyyy-MM");
        //获取当前年月日
        String specificDate = date.format(new Date());
        //获取当前年月
        String years = date1.format(new Date());
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        //将时间转成当前时间
        gregorianCalendar.setTime(new Date());
        //当前日期-1
        gregorianCalendar.add(gregorianCalendar.DATE,-1);
        //获取昨日时间并转成date样式
        String yesterday = date.format(gregorianCalendar.getTime());

        List<JsBeforeUser> allUsersRegistrationTime = homePageMapper.getAllUsersRegistrationTime();
        for (JsBeforeUser a:allUsersRegistrationTime) {
            AllUsers++;
            if (a.getCreateDate().indexOf(specificDate)!= -1){
                todayUser++;
            }
            if (a.getCreateDate().indexOf(yesterday)!= -1){
                yesterdayUser++;
            }
            if (a.getCreateDate().indexOf(years)!= -1){
                monthUser++;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("todayUser",todayUser);
        map.put("yesterdayUser",yesterdayUser);
        map.put("monthUser",monthUser);
        map.put("AllUsers",AllUsers);

        return map;
    }
    @Override
    public Map<String,Object> getAllCreateDate(Integer courseStatus,Integer courseCstatus) {
        Map<String, Object> map = new HashMap<>();
        int yi = 0;
        int er = 0;
        int san = 0;
        int si = 0;
        int wu = 0;
        int liu = 0;
        int qi = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar cal = Calendar.getInstance();
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Date mondayDate = cal.getTime();
        long time1 = mondayDate.getTime();
        String weekBegin = sdf.format(mondayDate);

        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        long time = sundayDate.getTime();
        String weekEnd = sdf.format(sundayDate);
        List<JsCourse> jsCourse = homePageMapper.getAllCreateDate(courseStatus,courseCstatus);
        String format = sdf1.format(new Date());
        ArrayList<Long> list = new ArrayList<>();
        list.add(time1);
        list.add(time1+86400000);
        list.add(time1+86400000+86400000);
        list.add(time1+86400000+86400000+86400000);
        list.add(time1+86400000+86400000+86400000+86400000);
        list.add(time1+86400000+86400000+86400000+86400000+86400000);
        list.add(time1+86400000+86400000+86400000+86400000+86400000+86400000);
        for (JsCourse j:jsCourse) {
            if (j.getCreateDate().indexOf(format)!=-1){
                try {
                    if (sdf2.parse(j.getCreateDate()).getTime()>= list.get(0)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(1)){
                        san++;
                        break;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(1)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(2)){
                        si++;
                        break;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(2)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(3)){
                        wu++;
                        break;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(3)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(4)){
                        liu++;
                        break;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(4)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(5)){
                        qi++;
                        break;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(5)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(6)){
                        yi++;
                        break;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(6)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(7)){
                        er++;
                        break;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        map.put("yi",yi);
        map.put("er",er);
        map.put("san",san);
        map.put("si",si);
        map.put("wu",wu);
        map.put("liu",liu);
        map.put("qi",qi);
        return map;
    }

    @Override
    public Map<String,Object> getAllProductCreateDate(Integer productSate) {
        Map<String, Object> map = new HashMap<>();
        int yi = 0;
        int er = 0;
        int san = 0;
        int si = 0;
        int wu = 0;
        int liu = 0;
        int qi = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar cal = Calendar.getInstance();
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Date mondayDate = cal.getTime();
        long time1 = mondayDate.getTime();
        String weekBegin = sdf.format(mondayDate);

        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        long time = sundayDate.getTime();
        String weekEnd = sdf.format(sundayDate);
        List<JsProduct> AllProductCreateDate =  homePageMapper.getAllProductCreateDate(productSate);
        String format = sdf1.format(new Date());
        ArrayList<Long> list = new ArrayList<>();
        list.add(time1);
        list.add(time1+86400000);
        list.add(time1+86400000+86400000);
        list.add(time1+86400000+86400000+86400000);
        list.add(time1+86400000+86400000+86400000+86400000);
        list.add(time1+86400000+86400000+86400000+86400000+86400000);
        list.add(time1+86400000+86400000+86400000+86400000+86400000+86400000);
        for (JsProduct j:AllProductCreateDate) {
            if (j.getCreateDate().indexOf(format)!=-1){
                try {
                    if (sdf2.parse(j.getCreateDate()).getTime()>= list.get(0)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(1)){
                        san++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(1)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(2)){
                        si++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(2)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(3)){
                        wu++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(3)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(4)){
                        liu++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(4)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(5)){
                        qi++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(5)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(6)){
                        yi++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(6)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(7)){
                        er++;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        map.put("yi",yi);
        map.put("er",er);
        map.put("san",san);
        map.put("si",si);
        map.put("wu",wu);
        map.put("liu",liu);
        map.put("qi",qi);
        return map;
    }

    @Override
    public Map<String, Object> getAllUserCreateDate() {
        Map<String, Object> map = new HashMap<>();
        int yi = 0;
        int er = 0;
        int san = 0;
        int si = 0;
        int wu = 0;
        int liu = 0;
        int qi = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar cal = Calendar.getInstance();
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Date mondayDate = cal.getTime();
        long time1 = mondayDate.getTime();
        String weekBegin = sdf.format(mondayDate);

        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        long time = sundayDate.getTime();
        String weekEnd = sdf.format(sundayDate);
        List<JsBeforeUser> AllUserCreateDate =  homePageMapper.getAllUserCreateDate();
        String format = sdf1.format(new Date());
        ArrayList<Long> list = new ArrayList<>();
        list.add(time1);
        list.add(time1+86400000);
        list.add(time1+86400000+86400000);
        list.add(time1+86400000+86400000+86400000);
        list.add(time1+86400000+86400000+86400000+86400000);
        list.add(time1+86400000+86400000+86400000+86400000+86400000);
        list.add(time1+86400000+86400000+86400000+86400000+86400000+86400000);
        for (JsBeforeUser j:AllUserCreateDate) {
            if (j.getCreateDate().indexOf(format)!=-1){
                try {
                    if (sdf2.parse(j.getCreateDate()).getTime()>= list.get(0)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(1)){
                        san++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(1)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(2)){
                        si++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(2)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(3)){
                        wu++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(3)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(4)){
                        liu++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(4)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(5)){
                        qi++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(5)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(6)){
                        yi++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(6)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(7)){
                        er++;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        map.put("yi",yi);
        map.put("er",er);
        map.put("san",san);
        map.put("si",si);
        map.put("wu",wu);
        map.put("liu",liu);
        map.put("qi",qi);
        return map;
    }

    @Override
    public Map<String, Object> TotalOfThisWeekAndLastWeekCreateDate(Integer courseStatus,Integer courseCstatus) {
        Map<String, Object> map = new HashMap<>();
        int thisWeek = 0;
        int lastWeek = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar cal = Calendar.getInstance();
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Date mondayDate = cal.getTime();
        long time1 = mondayDate.getTime();
        String weekBegin = sdf.format(mondayDate);

        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        long time = sundayDate.getTime();
        String weekEnd = sdf.format(sundayDate);

        List<JsCourse> jsCourse = homePageMapper.getAllCreateDate(courseStatus,courseCstatus);
        String format = sdf1.format(new Date());
        ArrayList<Long> list = new ArrayList<>();
        list.add(time1-604800000);
        list.add(time1);
        list.add(time);


        for (JsCourse j:jsCourse) {
            if (j.getCreateDate().indexOf(format)!=-1){
                try {
                    if (sdf2.parse(j.getCreateDate()).getTime()>= list.get(0)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(1)){
                        lastWeek++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(1)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(2)){
                        thisWeek++;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        map.put("lastWeek",lastWeek);
        map.put("thisWeek",thisWeek);
        return map;
    }

    @Override
    public Map<String, Object> TotalOfThisWeekAndLastWeekProductCreateDate(Integer productSate) {
        Map<String, Object> map = new HashMap<>();
        int thisWeek = 0;
        int lastWeek = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar cal = Calendar.getInstance();
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Date mondayDate = cal.getTime();
        long time1 = mondayDate.getTime();
        String weekBegin = sdf.format(mondayDate);

        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        long time = sundayDate.getTime();
        String weekEnd = sdf.format(sundayDate);

        List<JsProduct> jsProduct = homePageMapper.getAllProductCreateDate(productSate);
        String format = sdf1.format(new Date());
        ArrayList<Long> list = new ArrayList<>();
        list.add(time1-604800000);
        list.add(time1);
        list.add(time);


        for (JsProduct j:jsProduct) {
            if (j.getCreateDate().indexOf(format)!=-1){
                try {
                    if (sdf2.parse(j.getCreateDate()).getTime()>= list.get(0)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(1)){
                        lastWeek++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(1)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(2)){
                        thisWeek++;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        map.put("lastWeek",lastWeek);
        map.put("thisWeek",thisWeek);
        return map;
    }

    @Override
    public Map<String, Object> TotalOfThisWeekAndLastWeekUserCreateDate() {
        Map<String, Object> map = new HashMap<>();
        int thisWeek = 0;
        int lastWeek = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar cal = Calendar.getInstance();
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Date mondayDate = cal.getTime();
        long time1 = mondayDate.getTime();
        String weekBegin = sdf.format(mondayDate);

        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        long time = sundayDate.getTime();
        String weekEnd = sdf.format(sundayDate);

        List<JsBeforeUser> jsUser = homePageMapper.getAllUserCreateDate();
        String format = sdf1.format(new Date());
        ArrayList<Long> list = new ArrayList<>();
        list.add(time1-604800000);
        list.add(time1);
        list.add(time);


        for (JsBeforeUser j:jsUser) {
            if (j.getCreateDate().indexOf(format)!=-1){
                try {
                    if (sdf2.parse(j.getCreateDate()).getTime()>= list.get(0)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(1)){
                        lastWeek++;
                    }else if(sdf2.parse(j.getCreateDate()).getTime()>= list.get(1)&&sdf2.parse(j.getCreateDate()).getTime()< list.get(2)){
                        thisWeek++;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        map.put("lastWeek",lastWeek);
        map.put("thisWeek",thisWeek);
        return map;
    }

    @Override
    public boolean UpdateUser(JsUser param) {
        if (param.getPassword() != null){
            String password = param.getPassword();
            System.err.println(password);
            String s = MD5Util.md5(password);
            param.setPassword(s);
        }
        return homePageMapper.UpdateUser(param);
    }

    @Override
    public String getMysqlEdition() {
        return homePageMapper.getMysqlEdition();
    }

    @Override
    public Integer TotalAmountOfGoodsOnShelf(Integer type, Integer result, Integer productSate) {
        return homePageMapper.TotalAmountOfGoodsOnShelf(type,result,productSate);
    }

    @Override
    public Integer TotalVideoOverhead(Integer courseCstatus, Integer courseStatus) {
        return homePageMapper.TotalVideoOverhead(courseCstatus, courseStatus);
    }

    @Override
    public Integer TotalNumberOfPublishedMicrocircles(Integer details, Integer show) {
        return homePageMapper.TotalNumberOfPublishedMicrocircles(details,show);
    }

    @Override
    public Integer TotalNumberOfCompletedOrders(Integer orderStatus) {
        return homePageMapper.TotalNumberOfCompletedOrders(orderStatus);
    }

    @Override
    public Integer TotalProductStayExamine(Integer type, Integer result) {
        return homePageMapper.TotalProductStayExamine(type,result);
    }

    @Override
    public Integer TotalCourseStayExamine(Integer type, Integer result) {
        return homePageMapper.TotalCourseStayExamine(type,result);
    }

    @Override
    public Integer TotalMicroCircleStayExamine(Integer type, Integer result) {
        return homePageMapper.TotalMicroCircleStayExamine(type,result);
    }

    @Override
    public Integer TotalUnsubscribeStayExamine(Integer orderStatus) {
        return homePageMapper.TotalUnsubscribeStayExamine(orderStatus);
    }

    @Override
    public Integer getTotalProductSales(Integer orderType) {
        return homePageMapper.getTotalProductSales(orderType);
    }

    @Override
    public Integer getTotalVideoCollection(Integer collectionType) {
        return homePageMapper.getTotalVideoCollection(collectionType);
    }

    @Override
    public Integer getTotalNumberOfCompletedOrders(Integer orderType){
        return homePageMapper.getTotalNumberOfCompletedOrders(orderType);
    }

    @Override
    public Integer getTotalOrders(){
        return homePageMapper.getTotalOrders();
    }

    @Override
    public Integer getCourseBrowseTotal() {
        return homePageMapper.getCourseBrowseTotal();
    }

    @Override
    public Integer getTotalVideoLikes() {
        return homePageMapper.getTotalVideoLikes();
    }

    @Override
    public Integer getMicroCircleFabulousCount(Integer details,Integer show) {
        return homePageMapper.getMicroCircleFabulousCount(details,show);
    }

    @Override
    public Integer getTotalWebUser(Integer status){
        return homePageMapper.getTotalWebUser(status);
    }

    @Override
    public Integer getTotalBackstageUser(Integer status) {
        return homePageMapper.getTotalBackstageUser(status);
    }
    @Override
    public Integer getCreatorExmaine(Integer result) {
        return homePageMapper.getCreatorExmaine(result);
    }

    @Override
    public Integer getTotalJinsiUser() {
        return homePageMapper.getTotalJinsiUser() ;
    }

    @Override
    public Integer getUserGrade(Integer userId) {
        return homePageMapper.getUserGrade(userId);
    }


}
